
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mini.h"

// test stack
void test_stack()
{
    struct stack_t *s = stack_create(10);

    int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    fprintf(stderr, "test stack\n");

    for(int i=0; i<10; i++) {
        stack_push(s, &num[i]);
    }

    int length = stack_length(s);

    int isfull = stack_isfull(s);

    stack_realloc(s, 15);

    int *p;
    for(int i=0; i<10; i++) {
        p = stack_pop(s);
    }

    return;
}

// test queue
void test_queue()
{
    struct queue_t *q = queue_create(10);

    int num[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    fprintf(stderr, "test queue\n");

    for(int i=0; i<10; i++) {
        queue_add(q, &num[i]);
    }

    int length = queue_length(q);

    int isfull = queue_isfull(q);

    queue_realloc(q, 15);

    int *p;
    for(int i=0; i<10; i++) {
        p = queue_del(q);
    }

    return;
}

void display(void *data, void *arg)
{
    fprintf(stderr, "data val : %d\n",  *((int *)data) );
}

void display2(void *data, void *arg)
{
    fprintf(stderr, "data addr : %p\n", data);
}

// test slist
void test_slist()
{
    struct slist_t sl;
    int num[6] = {1, 2, 3, 4, 5, 6};

    fprintf(stderr, "test slist\n");

    slist_init(&sl);

    for(int i=0; i<4; i++) {
        slist_append(&sl, &num[i]);
    }

    slist_insert(&sl, &num[1], &num[5]);

    int length = slist_length(&sl);
    struct slnode_t *n = slist_find(&sl, &num[3]);

    slist_foreach(&sl, (slist_func_t)display, NULL);
    slist_foreach(&sl, (slist_func_t)display2, NULL);

    for(int i=0; i<5; i++) {
        slist_delete(&sl, &num[i]);
    }

    return;
}

// test dlist
void test_dlist()
{
    struct dlist_t dl;
    int num[5] = { 1, 2, 3, 4, 5};

    fprintf(stderr, "test dlist\n");

    dlist_init(&dl);

    for(int i=0; i<5; i++) {
        dlist_append(&dl, &num[i]);
    }

    dlist_insert(&dl, &num[1], &num[3]);

    int length = dlist_length(&dl);
    struct dlnode_t *n = dlist_find(&dl, &num[3]);

    dlist_foreach(&dl, (dlist_func_t)display, NULL);
    dlist_foreach(&dl, (dlist_func_t)display2, NULL);

    for(int i=0; i<5; i++) {
        dlist_delete(&dl, &num[i]);
    }

    return;
}

int btree_compress(void *d1, void *d2)
{
    int n1 = *((int *)d1);
    int n2 = *((int *)d2);

    int val = n2-n1;

    if(val < 0) {
        return -1;
    } if(val > 0) {
        return 1;
    } else {
        return 0;
    }
}

void test_btree()
{
    struct btree_t bt;
    int num[5] = { 1,2,3,4,5};

    fprintf(stderr, "test btree\n");

    btree_init(&bt, (btree_cmp_t)btree_compress);

    for(int i=0; i<5; i++) {
        btree_add(&bt, &num[i]);
    }

    btree_foreach(bt.root, (btree_func_t)display, NULL);

    struct btnode_t *n = btree_find(&bt, &num[2]);

    for(int i=0; i<5; i++) {
        btree_delete(&bt, &num[i]);
        btree_foreach(bt.root, (btree_func_t)display, NULL);
    }

    return;
}

int main(int argc, char *argv[])
{

    printf("hello world\n");

    test_stack();

    test_queue();

    test_slist();

    test_dlist();

    test_btree();

    return 0;
}
